<?php
// ------------------------------------------------------------------------------------
// Bestand:      registrercontroller.php
// Versie:        1.0
// Projekt:       Cursus MasterClass van Eduvision
// Datum:         11-06-2008
// Auteur(s):     Richard lagendijk
// Email(s):      r.lagendijk@hccnet.nl
// Revisie(s):
//
// Beschrijving:  Login pagina van de webpagina: Askkid-CF.
// ------------------------------------------------------------------------------------
//
require_once 'Controller.php';
require_once 'User.php';
//require_once 'clsCheckInput.php';
require_once 'DataBase.php';
require_once 'MailMessage.php';

class registerController extends Controller 
{
  private $user;
  
  public function __construct () 
  {
  	parent::__construct();
  }
  
  public function index ($p_sParameter = NULL) 
  {
  	parent::assign('firstquestion',$p_sParameter);	
    parent::render('register.tpl');
  }
  
  public function newRegistration()
  {
  		//get insert vars
  		$_sUsername = $_POST['username'];
  		$_sRealname = $_POST['realname'];
  		$_sEmail = $_POST['emailaddress'];
  		$_sBirthdate = $_POST['birthdate'];
  		$_sPassword1 = $_POST['password_1'];
  		$_sPassword2 = $_POST['password_2'];
  		$_sRegIP = $_SERVER['REMOTE_ADDR'];
  		$_sRegDate = time();
  		$_sConfirmhash = sha1($_sRegDate . $_sRealname);
  		$_iFirstQuestion = $_POST['firstquestion'];
		
  		//check input
  		//@todo via ajax!
  				
  		//insert user into database
  		$DB = DataBase::getInstance();
  		$output = $DB->runDirectQuery("INSERT INTO Users (regdate, realname, email, birthdate, regip, verification, role, password, username, registerhash, firstquestion) VALUES ('$_sRegDate', '$_sRealname', '$_sEmail', '$_sBirthdate', '$_sRegIP', '0', 'guest', '" . md5($_sPassword1) . "', '$_sUsername', '$_sConfirmhash', '$_iFirstQuestion')");
  		
  		//send confirmmail
  		$mailmessage = new MailMessage();
  		
  		$mailmessage->assign('realname',$_sRealname);
  		$mailmessage->assign('mailvalidationcode',$output->getRecordID(). ';' . $_sConfirmhash);
  		  	  		
 		$mailmessage->setMessage('mail_NewUserWelcome.tpl');
  		$mailmessage->setSubject('Welkom op Askkid!');
		$mailmessage->addRecipient($_sEmail);  
		$mailmessage->setMessageType('HTML');	
		$mailmessage->send();
		
  		//show confirmscreen
		
  		parent::assign('melding',"Net is er een email verstuurt naar het door jou opgegeven adres! \n Hierin staat een link waarop moet worden geklikt om alles af te maken! \n Dit mailtje is gestuurd naar dit adres:");
  		parent::assign('name',$_sRealname);
  		parent::assign('email',$_sEmail);
		  		
 		parent::render('site_register_confirm.tpl');
  	}
  	
  	public function checkHash($p_sParameter)
  	{
  		$parameters = explode(';', $p_sParameter);
  		
  		$user = new User();

  		$user->setByParentId($parameters[0]);
  		//$sql = "SELECT username,registerhash FROM Users where username = '$parameters[0]'";
  		
		//$param = mysql_fetch_object($db->runQuery($sql));

		if(strcmp($user->getRegisterHash(),$parameters[1])!= 0)
		{
			parent::assign('melding','Je hebt proberen te registreren met een verkeerde code!!');
  			parent::assign('name',$user->getRealname());
		}
		else
		{
			$user->setRegisterHash(null);
			
			$_SESSION['user'] = $user->getId();
			
  			parent::assign('melding','De registratie is helemaal klaar en je bent voor de eerste keer ingelogd!! Veel plezier op de site!');
  			parent::assign('name',$user->getRealname());
		}
		
		//$sql = "UPDATE Users SET registerhash = '' WHERE username = '$parameters[0]'";
		//$db->runQuery($sql);
		  		
 		parent::render('site_register_confirm.tpl');
  	}
  		
}
?>
